package com.example.proxy3_cglib;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibProxy implements MethodInterceptor {
    private Object target;
    CglibProxy(Object target) {
        this.target = target;
    }

    public boolean conn() {
        System.out.println("connecting");
        return true;
    }

    public void close() {
        System.out.println("closing.");
    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Object ret = null;
        if (this.conn()) {
            ret = method.invoke(this.target, args);
            this.close();
        }
        return ret;
    }
}
